<?php
namespace App\Http\Controllers\Front;


use App\Models\Achievement;
use App\Models\Attendance;
use App\Models\AttendanceSet;
use App\Models\Feedback;
use App\Models\FeedbackDetail;
use App\Models\FeedbackSetDetail;
use App\Models\Group;
use App\Models\Message;
use App\Models\Order;
use App\Models\RewardPunishDetail;
use App\Models\RewardPunishSet;
use App\Models\RewardPunishSetDetail;
use App\Models\Tips;
use App\Models\UserPerformance;
use App\Models\UserPerformanceMonth;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Redis;
use App\Models\Keys;
use Curl\Curl;
use App\Models\CorpInfo;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use App\Models\RoleUser;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Input;
use App\Models\Company;
use App\Models\Register;
use App\Models\RegUser;
use App\Models\Code;


class LoginController extends Controller
{

    public function __construct()
    {
        $this->middleware('account', ['except' => [ 'getLogin','getRegister', 'getFindPassword',
            'postPasswordCode','postResetPassword', 'postRegister',
            'postLogin','postLogin1','getAdmin','postLoginadmin','getMoni']]);
    }


    public function getXXX(Request $request)
    {
        dd(2);
        $users = Company::all();
        foreach ($users as $u){
            $years = implode(',',[$u->from_year]);
            $u->years_array = $years;
            $u->save();
        }

        dd(2);

    }

    //模拟用户登录，方便调试20190702
    public function getMoni(Request $request)
    {
        $id = $request->input('id');
        $password = $request->input('password');
        if(empty($id) || empty($password)){
            abort('403','参数错误');
        }
        if($password != 'cimers123'){
            abort('403','参数错误');
        }
        $user = Register::find($id);
        if(empty($user)){
            abort('403','参数错误');
        }
        Auth::login($user);
        return redirect('/jump');
    }
    
    //登录首页
    public function getLogin()
    {
        if(is_mobile()){
            $view = 'login.index';
        }else{
            $view = 'pc_login.index';
        }
        return view($view);
    }


    //登录首页
    public function getAdmin()
    {
        if(is_mobile()){
            $view = 'pc_login.admin-index';
        }else{
            $view = 'pc_login.admin-index';
        }
        return view($view);
    }
    

    //注册首页
    public function getRegister()
    {
        if(is_mobile()){
            $view = 'login.register';
        }else{
            $view = 'pc_login.register';
        }

        return view($view);
    }


    //找回密码页
    public function getFindPassword()
    {
        if(is_mobile()){
            $view = 'login.password';
        }else{
            $view = 'pc_login.password';
        }
        return view($view);
    }


    //验证找回密码验证码
    public function postPasswordCode()
    {
        $result = array(
            'status'        => 'fail',
            'msg'           => '验证成功',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $data = Input::only('username',  'phone_code');
        $nowtime = date('Y-m-d H:i:s');
        $isConti = Code::where('created_at', '<', date('Y-m-d H:i:s', time()))
            ->where('created_at', '>', date('Y-m-d H:i:s', strtotime("$nowtime - 10 minute")))
            ->where( 'code',$data['phone_code'])
            ->where( 'is_used',0)
            ->where( 'type',1)
            ->where('phone',$data['username'])
            ->first();
        if (empty($isConti)) {
            $result['msg'] = '验证码不正确';
            return Response::json($result);
        }else{
            $result['status'] = 'success';
            $result['msg'] = '验证成功';
            return Response::json($result);
        }
    }



    //修改密码
    public function postResetPassword()
    {
        $result = array(
            'status'        => 'fail',
            'msg'           => '修改密码成功',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $data = Input::only('username', 'password', 'repassword', 'phone_code');
        if (empty(trim($data['password']))) {
            $result['msg'] = '密码不能为空';
            return Response::json($result);
        }
        if (empty(trim($data['repassword']))) {
            $result['msg'] = '再次输入密码不能为空';
            return Response::json($result);
        }
        if (trim($data['repassword']) !== trim($data['password'])) {
            $result['msg'] = '密码不一致';
            return Response::json($result);
        }
        if (mb_strlen($data['password']) < 6 || mb_strlen($data['password']) > 16) {
            $result['msg'] = '密码长度不符合要求，6-16位';
            return Response::json($result);
        }
        $nowtime = date('Y-m-d H:i:s');
        $isConti = Code::where('created_at', '<', date('Y-m-d H:i:s', time()))
            ->where('created_at', '>', date('Y-m-d H:i:s', strtotime("$nowtime - 10 minute")))
            ->where( 'code',$data['phone_code'])
            ->where( 'is_used',0)
            ->where( 'type',1)
            ->where('phone',$data['username'])
            ->first();
        if (empty($isConti)) {
            $result['msg'] = '验证码不正确';
            return Response::json($result);
        }
        $user = Register::where('phone',$data['username'])->orWhere('email',$data['username'])->first();
        $user->password = password_hash(trim($data['password']), PASSWORD_DEFAULT);
        $user->save();
        $isConti->is_used = 1;
        $isConti->save();
        $result['status'] = 'success';
        $result['redirectUrl'] = '/front/login';
        return Response::json($result);
        
    }


    //提交注册
    public function postRegister()
    {
        $result = array(
            'status'        => 'fail',
            'msg'           => '注册成功',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $registerUser = array();
        $data = Input::only('username', 'password', 'repassword', 'phone_code');

        if (empty(trim($data['username']))) {
            $result['status'] = 'fail';
            $result['msg'] = '用户名有误';
            return Response::json($result);
        }
        $hasUser = Register::Where('phone', '=', $data['username'])->orWhere('email', '=', $data['username'])->first();
        if (!empty($hasUser) && $hasUser->id > 0) {
            $result['msg'] = '用户名已存在';
            return Response::json($result);
        }
        if (preg_match(config('app.phone_rule'), $data['username'])) {
            $registerUser['phone'] = $data['username'];
        } elseif (preg_match(config('app.email_rule'), $data['username'])) {
            $registerUser['email'] = $data['username'];
        } else {
            $result['msg'] = '用户名格式有误，请输入正确邮箱或手机号';
            return Response::json($result);
        }
        if (empty(trim($data['password']))) {
            $result['msg'] = '密码不能为空';
            return Response::json($result);
        }
        if (empty(trim($data['repassword']))) {
            $result['msg'] = '再次输入密码不能为空';
            return Response::json($result);
        }
        if (trim($data['repassword']) !== trim($data['password'])) {
            $result['msg'] = '密码不一致';
            return Response::json($result);
        }
        if (mb_strlen($data['password']) < 6 || mb_strlen($data['password']) > 16) {
            $result['msg'] = '密码长度不符合要求，6-16位';
            return Response::json($result);
        }
        $nowtime = date('Y-m-d H:i:s');
        $isConti = Code::where('created_at', '<', date('Y-m-d H:i:s', time()))
            ->where('created_at', '>', date('Y-m-d H:i:s', strtotime("$nowtime - 10 minute")))
            ->where(['phone' => $data['username'], 'code' => $data['phone_code'], 'is_used' => 0])->first();
        if (empty($isConti)) {
            $result['msg'] = '验证码不正确';
            return Response::json($result);
        }
        $registerUser['name'] = $data['username'];
        $registerUser['password'] = password_hash(trim($data['password']), PASSWORD_DEFAULT);
            DB::beginTransaction();
            try {
                $isConti->update(['is_used' => 1]);
                $newuser = Register::firstOrCreate($registerUser);
                DB::commit();
                Auth::login($newuser);

            } catch (\PDOException $e) {
                Log::info('个人用户注册失败错误 ' . $e->getMessage());
                DB::rollBack();
                $result['msg'] = '注册失败';
                return Response::json($result);
            }
        $result['status'] = 'success';
        $result['redirectUrl'] = '/jump';
        return Response::json($result);
    }


    //登录
    public function postLogin()
    {
        $result = array(
            'status'        => 'success',
            'msg'           => '登录成功',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $data = Input::only(['username', 'password','is_remember']);
        $user = Register::Where('phone', '=', $data['username'])->orWhere('email', '=', $data['username'])->orWhere('name', '=', $data['username'])->first();
        if (!empty($user) && count($user) > 0) {
            $login = 'name';
            if (preg_match(config('app.phone_rule'), $data['username'])) {
                $login = 'phone';
            } elseif (preg_match(config('app.email_rule'), $data['username'])) {
                $login = 'email';
            }
            if($data['is_remember'] == 'on' || $data['is_remember'] == true)
            {
                $remember = true;
            }else{
                $remember = false;
            }

            if (Auth::attempt([$login => $data['username'], 'password' => $data['password']],$remember)){
                Auth::login($user);
                $reguser = RegUser::where('register_id',$user->id)->where('company_status','1')->whereIn('status',[1,3])->first();
                if(empty($reguser)){
                    $result['redirectUrl']  = '/jump';
                    return Response::json($result);
                }else{
                    if($reguser->status == 3){
                        $result['redirectUrl']  = '/front/wait';
                        return Response::json($result);
                    }else{
                        $result['redirectUrl']  = '/jump';
                        return Response::json($result);
                    }

                }

            } else {
                $result['status'] = 'fail';
                $result['msg'] = '登录失败,密码不正确';
                return Response::json($result);
            }
        } else {
            $result['status'] = 'fail';
            $result['msg'] = '登录失败,用户不存在';
            return Response::json($result);
        }
    }


    //pc端登录，仅限拥有管理员的角色
    public function postLogin1()
    {
        $result = array(
            'status'        => 'success',
            'msg'           => '登录成功',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $data = Input::only(['username', 'password','is_remember']);
        $user = Register::Where('name', '=', $data['username'])->orWhere('email', '=', $data['username'])->first();
        if (!empty($user) && count($user) > 0) {
            $login = 'name';
            if (preg_match(config('app.phone_rule'), $data['username'])) {
                $login = 'phone';
            } elseif (preg_match(config('app.email_rule'), $data['username'])) {
                $login = 'email';
            }
            if($data['is_remember'] == 'on' || $data['is_remember'] == true)
            {
                $remember = true;
            }else{
                $remember = false;
            }

            if (Auth::attempt([$login => $data['username'], 'password' => $data['password']],$remember)){
                $reguser = RegUser::where('register_id',$user->id)->where('status','1')->where('company_status','1')->first();
                if(empty($reguser)){
                    $result['status'] = 'fail';
                    $result['msg'] = '您尚未加入企业，请在手机端登录操作';
                    return Response::json($result);
                }else{
                    $usid = $reguser->user_id;
                    $uuser = User::find($usid);
                    if(empty($uuser)){
                        $result['status'] = 'fail';
                        $result['msg'] = '您尚未加入企业，请在手机端登录操作';
                        return Response::json($result);
                    }
                    if($uuser->status != 1){
                        $result['status'] = 'fail';
                        $result['msg'] = '您已不在企业中，请在手机端登录操作';
                        return Response::json($result);
                    }
                    $hasrole = RoleUser::where('role_id','9')->where('corpid',$reguser->corpid)->where('user_id',$usid)->first();
                    if(empty($hasrole)){
                        $result['status'] = 'fail';
                        $result['msg'] = '您不是企业系统管理员，请在手机端登录操作';
                        return Response::json($result);
                    }
                    $com = Company::where('id',$reguser->corpid)->first();
                    if(empty($com) || $com->init_index != 2){
                        $result['status'] = 'fail';
                        $result['msg'] = '请先在手机端初始化您的企业信息';
                        return Response::json($result);
                    }
                    if(time() > strtotime($com->expire_at)){
                        Auth::login($user);
                        return redirect('/admin/buy-vip');
                    }
                    Auth::login($user);
                    $result['redirectUrl']  = '/jump';
                    return Response::json($result);

                }


            } else {
                $result['status'] = 'fail';
                $result['msg'] = '登录失败,密码不正确';
                return Response::json($result);
            }
        } else {
            $result['status'] = 'fail';
            $result['msg'] = '登录失败,用户不存在';
            return Response::json($result);
        }
    }

    /**
     * 青苜管理员登录
     */
    public function postLoginadmin()
    {
        $result = array(
            'status'        => 'success',
            'msg'           => '登录成功',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $data = Input::only(['username', 'password','is_remember']);
        $user = Register::Where('name', '=', $data['username'])->where('type', '=','3')->first();
        if (!empty($user) && count($user) > 0) {
            $login = 'name';
            if (preg_match(config('app.phone_rule'), $data['username'])) {
                $login = 'phone';
            } elseif (preg_match(config('app.email_rule'), $data['username'])) {
                $login = 'email';
            }
            if($data['is_remember'] == 'on' || $data['is_remember'] == true)
            {
                $remember = true;
            }else{
                $remember = false;
            }

            if (Auth::attempt([$login => $data['username'], 'password' => $data['password']],$remember)){
                Auth::login($user);
                $result['redirectUrl']  = '/cimo/corplist';
                return Response::json($result);

            } else {
                $result['status'] = 'fail';
                $result['msg'] = '登录失败,密码不正确';
                return Response::json($result);
            }
        } else {
            $result['status'] = 'fail';
            $result['msg'] = '登录失败,用户不存在';
            return Response::json($result);
        }
    }


    //登录后未加入企业
    public function getStart()
    {
        $regu = RegUser::where('register_id',Auth::user()->id)->where('company_status','1')->whereIn('status',['1','3'])->first();
        if(!empty($regu)){
            if($regu->status == 1){
                //return redirect('/jump');
                $view = 'login.create-add';
                return view($view);
            }
            if($regu->status == 3){
                return redirect('/front/wait');
            }
        }else{
            $view = 'login.create-add';
            return view($view);
        }
    }


    //申请后等待企业审核通过
    public function getWait()
    {
        $regu = RegUser::where('register_id',Auth::user()->id)->where('company_status','1')->whereIn('status',['1','3'])->first();
        if(empty($regu)){
            return redirect('front/start');
        }
        if($regu->status == 1){
            return redirect('/jump');
        }
        $user = Auth::user();
        $apply = RegUser::where('register_id',$user->id)->where('status','3')->first();
        $company = Company::where('id',$apply->corpid)->first();
        $view = 'login.cancle-apply';
        return view($view)
            ->with('company',$company)
            ->with('apply',$apply);
    }

    

    //搜索，加入企业
    public function getSearch(Request $request)
    {
        if(!empty(Auth::user()->comuser())){
            return redirect('/jump');
        }
        $name = $request->input('name','');
        $name = trim($name);
        $view = 'login.apply-company';
        if(empty($name)){
            return view($view);
        }
        $company = Company::where('corpid','like','web_company_%')->where('name','like','%'.$name.'%')->get();
        return view($view)
            ->with('company',$company);
    }


    /*//搜索，显示企业
    public function postSearch(Request $request)
    {
        $name = $request->input('name');
        $com = Company::where('name','like','%'.$name.'%')->get();
        $view = '';
        return view($view)
            ->with('company',$com);
    }*/


    //加入企业
    public function postJoinCompany(Request $request)
    {
        if(!empty(Auth::user()->comuser())){
            return redirect('/jump');
        }
        $result = array(
            'status'        => 'fail',
            'msg'           => '申请失败',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $user = Auth::user();
        $comid = $request->input('comid',0);
        if(empty($comid) || $comid <= 0){
            $result['msg'] = '公司id不正确';
            return Response::json($result);
        }
        $quote = $request->input('quote','');
        if(empty($quote)){
            $result['msg'] = '附言不能为空';
            return Response::json($result);
        }
        if(mb_strlen($quote,'UTF8') > 12 && mb_strlen($quote) >30){
            $result['msg'] = '附言太长';
            return Response::json($result);
        }
        $corp = Company::find($comid);
        if(empty($corp)){
            $result['msg'] = '未找到该公司信息';
            return Response::json($result);
        }
        //$comid = $corp->corpid;
        //是否存在以前申请记录
        $reguser = RegUser::where('register_id',$user->id)->where('corpid',$comid)->first();
        if(empty($reguser)){
            DB::beginTransaction();
            try {
                $loginuser = new User();
                $loginuser->qw_userid = $user->name;
                $loginuser->corpid = $comid;
                $loginuser->old_corp_no = $corp->corpid;
                $loginuser->qw_name = $user->name;
                $loginuser->status = 3;
                $loginuser->save();

                $reguser = new RegUser();
                $reguser->register_id = $user->id;
                $reguser->status = 3;
                $reguser->quote = $quote;
                $reguser->user_id = $loginuser->id;
                $reguser->corpid = $comid;
                $reguser->old_corp_no = $corp->corpid;
                $reguser->created_at =  date('Y-m-d H:i:s');
                $reguser->save();

                DB::commit();
                $result['status'] = 'success';
                $result['msg'] = '申请成功';
                $result['redirectUrl'] = '/front/wait';
                return Response::json($result);
            } catch (\PDOException $e) {
                Log::info('申请加入企业失败错误 ' . $e->getMessage());
                DB::rollBack();
                $result['msg'] = '申请失败';
                return Response::json($result);
            }
            
        }else{
            //重置为正常状态
            $seconduser = User::find($reguser->user_id);
            $role = RoleUser::where('role_id','9')->where('user_id',$reguser->user_id)->where('corpid',$reguser->corpid)->first();
            if(!empty($role)){
                $seconduser->status = 1;
                $seconduser->save();
                $reguser->status = 1;//管理员直接通过
                $reguser->save();
                $result['status'] = 'success';
                $result['msg'] = '您是系统管理员，无须审核，直接通过';
                $result['redirectUrl'] = '/sysadmin/permission/index';
                return Response::json($result);
            }else{
                $seconduser->status = 3;
                $seconduser->save();
                $reguser->status = 3;//重置为申请状态
                $reguser->quote = $quote;
                $reguser->save();
                $result['status'] = 'success';
                $result['msg'] = '申请成功';
                $result['redirectUrl'] = '/front/wait';
                return Response::json($result);
            }

        }

    }


    //取消申请
    public function postCancelApply(Request $request)
    {
        /*if(!empty(Auth::user()->comuser())){
            return redirect('/jump');
        }*/
        $result = array(
            'status'        => 'fail',
            'msg'           => '申请失败',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $aid = $request->input('applyid',0);
        $user = Auth::user();
        if($aid <= 0){
            $result['msg'] = 'id不正确';
            return Response::json($result);
        }
        $reguser = RegUser::find($aid);
        if(empty($reguser)){
            $result['msg'] = '申请不存在';
            return Response::json($result);
        }
        $reguser->status = 2;
        $reguser->save();
        $seconduser = User::find($reguser->user_id);
        $seconduser->status = 2;
        $seconduser->save();

        $result['status'] = 'success';
        $result['msg'] = '成功';
        $result['redirectUrl'] = '/jump';
        return Response::json($result);

    }


    //放弃创建企业
    public function getGiveupCreateCom()
    {
        $first_user = Auth::user()->comuser();
        if(empty($first_user)){
            $reg = RegUser::where('register_id',Auth::user()->id)->where('company_status','0')->first();
            if(empty($reg)){
                return redirect('/jump');
            }
            $reg->status = 2;//退出
            $reg->company_status = 2;//企业作废
            $reg->save();
            return redirect('/jump');
        }
        $reg = RegUser::where('register_id',Auth::user()->id)->where('user_id',$first_user->id)->where('company_status','1')->first();
        if(empty($reg)){
            return redirect('/jump');
        }
        $reg->status = 2;//退出
        $reg->company_status = 2;
        $reg->save();
        return redirect('/jump');
    }


    //创建企业信息
    public function getCreateCompany()
    {
        return view('login.create-company');
    }


    //创建企业点击下一步
    public function getCreateNext()
    {
        $first_user = Auth::user();
        $seuser = $first_user->comuser();
        if(!empty($seuser)){
            return redirect('/admin/init/create');
        }
        $reg = RegUser::where('register_id',$first_user->id)->where('status','1')->where('company_status','0')->first();//有未完善的企业信息
        if(!empty($reg))
        {
            //$reg->company_status = 1;
            //$reg->save();
            return redirect('/admin/init/create');
        }
        $corpid = 'web_company_'.$first_user->id.'_'.time();
        DB::beginTransaction();
        try {

            $loginuser = new User();
            $loginuser->qw_userid = $first_user->name;
            $loginuser->old_corp_no = $corpid;
            $loginuser->qw_name = $first_user->name;
            $loginuser->status = 1;
            $loginuser->save();

            $reguser = new RegUser();
            $reguser->register_id = $first_user->id;
            $reguser->old_corp_no = $corpid;
            $reguser->status = 1;
            $reguser->user_id = $loginuser->id;
            $reguser->company_status = 1;
            $reguser->created_at = date('Y-m-d H:i:s');
            $reguser->save();

            //用户为授权管理员，赋予系统管理员权限
            $role = new RoleUser();
            $role->old_corp_no = $corpid;
            $role->user_id = $loginuser->id;
            $role->role_id = '9';
            $role->save();

            //新建默认绩效记录
            $perf = new UserPerformance();
            $perf->user_id = $loginuser->id;
            $perf->old_corp_no = $corpid;
            $perf->save();
            DB::commit();

            return redirect('/admin/init/create');
        } catch (\PDOException $e) {
            Log::info('创建企业初始化失败 ' . $e->getMessage());
            DB::rollBack();
            abort(403,'申请失败,创建企业初始化失败');
        }
    }



    //退出企业
    public function postQuitCompany(Request $request)
    {
        $result = array(
            'status'        => 'fail',
            'msg'           => '退出成功',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $cid = $request->input('comid',0);
        $user = Auth::user();
        if(empty($user)){
            $result['msg'] = '登录状态异常';
            return Response::json($result);
        }
        if($cid <= 0){
            $result['msg'] = 'id不正确';
            return Response::json($result);
        }
        $comp = Company::find($cid);
        if(empty($comp)){
            $result['msg'] = '公司信息异常';
            return Response::json($result);
        }
        $reguser = RegUser::where('register_id',$user->id)->where('corpid',$comp->id)->first();
        $reguser->status = 2;
        $reguser->save();

        $comuser = User::find($reguser->user_id);
        $comuser->status = 2;
        $comuser->save();

        $result['status'] = 'success';
        $result['msg'] = '成功';
        $result['redirectUrl'] = '/jump';
        return Response::json($result);

    }


    //个人设置-所属企业
    public function getMyCompany()
    {
        if(empty(Auth::user()->comuser())){
            abort('404');
        }
        $company = Company::where('id',Auth::user()->comuser()->corpid)->first();
        if(is_mobile()){
            $view = 'frontuser.user-company';
        }else{
            $view = 'pc_sysadmin.user-company';
        }
        return view($view)->with('company',$company);
    }


    /*//生成第三方对应的register
    public function getShengcheng()
    {
        $user = User::all();
        foreach ($user as $u){
            $newregister = new Register();
            $newregister->name = 'qw_'.$u->qw_userid.$u->id;
            $newregister->visual_password = '123456';
            $newregister->password = password_hash('123456', PASSWORD_DEFAULT);
            $newregister->type = 1;
            $newregister->save();

            $new_reguser = new RegUser();
            $new_reguser->register_id = $newregister->id;
            $new_reguser->user_id = $u->id;
            $new_reguser->corpid = $u->corpid;
            $new_reguser->status = 1;
            $new_reguser->created_at =  date('Y-m-d H:i:s');
            $new_reguser->save();
            echo 'success';
        }

    }*/


    //修改昵称，仅限web注册用户
    public function postModifyNickname(Request $request)
    {
        $result = array(
            'status'        => 'fail',
            'msg'           => '修改成功',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $name = $request->input('name','');
        $user = Auth::user();
        if(empty($user)){
            $result['msg'] = '登录状态异常';
            return Response::json($result);
        }
        $comuser = $user->comuser();
        if(empty($comuser)){
            $result['msg'] = '登录状态异常';
            return Response::json($result);
        }
        if(empty($name)){
            $result['msg'] = '昵称不能为空';
            return Response::json($result);
        }
        if(mb_strlen($name,'UTF8') > 7 && mb_strlen($name) >15){
            $result['msg'] = '昵称过长';
            return Response::json($result);
        }
        $comuser->qw_name = preg_replace('# #','',$name);
        $comuser->save();

        $result['status'] = 'success';
        $result['msg'] = '成功';
        return Response::json($result);

    }


    //修改密码，针对企业微信系统派发的默认密码
    public function postModifyQwPass(Request $request)
    {
        $result = array(
            'status'        => 'fail',
            'msg'           => '修改成功',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $password = $request->input('password','');
        $user = Auth::user();
        if(empty($user)){
            $result['msg'] = '登录状态异常';
            return Response::json($result);
        }
        $comuser = $user->comuser();
        if(empty($comuser)){
            $result['msg'] = '登录状态异常';
            return Response::json($result);
        }
        if (empty(trim($password))) {
            $result['msg'] = '密码不能为空';
            return Response::json($result);
        }
        if (mb_strlen($password) < 6 || mb_strlen($password) > 16) {
            $result['msg'] = '密码长度不符合要求，6-16位';
            return Response::json($result);
        }
        $user->password = password_hash(trim($password), PASSWORD_DEFAULT);
        $user->visual_password = trim($password);
        $user->save();

        $result['status'] = 'success';
        $result['msg'] = '成功';
        return Response::json($result);

    }

    //退出登录
    public function getUserLogout()
    {
        Auth::logout();
        return redirect('/front/login');
    }


    //企业微信购买提示
    public function getQwBuy()
    {
        $company = Company::where('id',Auth::user()->comuser()->corpid)->first();
        return view('sysadmin.qw-vip')->with('company',$company);
    }


    //换肤
    public function postChangeSkin(Request $request)
    {
        if(empty(Auth::user()->comuser())){
            return redirect('/jump');
        }
        $result = array(
            'status'        => 'fail',
            'msg'           => '换肤失败',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $skinid = $request->input('skinid',1);
        $user = Auth::user()->comuser();
        if($skinid != 1 && $skinid !=2 && $skinid !=3){
            $result['msg'] = 'id不正确';
            return Response::json($result);
        }
        $user->skinid = $skinid;
        $user->save();

        $result['status'] = 'success';
        $result['msg'] = '成功';
        $result['redirectUrl'] = '';
        return Response::json($result);

    }

  
}

